package com.weic.manager.article.controller;

import com.weic.manager.article.service.ExpertArticleService;
import com.weic.manager.controller.BaseController;
import com.weic.manager.utils.WUtils;
import com.weic.manager.utils.WebUtils;
import com.weic.manager.vo.ExpertArticleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * 文章Controller类
 */
@Controller
@RequestMapping(value = "/expert_article")
public class ExpertArticleController extends BaseController {

    @Autowired
    ExpertArticleService expertArticleService;

    @RequestMapping(value = "/article_list.html")
    public ModelAndView articleList(HttpServletRequest request , HttpServletResponse response){

        return new ModelAndView("/article_list.html");
    }

    @RequestMapping(value = "/artileDetail.html")
    public ModelAndView artileDetail(HttpServletRequest request , HttpServletResponse response){
        String pkExpertArticle = request.getParameter("pkExpertArticle");
        request.setAttribute("pkExpertArticle",pkExpertArticle);
        if(!WUtils.isEmpty(WebUtils.getLoginInfo()) && !WUtils.isEmpty(WebUtils.getLoginInfo().getUserVO())){
            request.setAttribute("userVO",WebUtils.getLoginInfo().getUserVO());
        }
        return new ModelAndView("/article.jsp");
    }

    @RequestMapping(value = "/getArtileDetail.json")
    public @ResponseBody Map<String,Object> getArtileDetail(HttpServletRequest request , HttpServletResponse response) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf8");
        String pk_expert_article = request.getParameter("pk_expert_article");
        ExpertArticleVO expertArticalVO = expertArticleService.selectByPK(pk_expert_article);
        Map<String,Object> returnMap = new HashMap<>();
        returnMap.put("expertArticalVO",expertArticalVO);
        returnMap.put("content",new String(expertArticalVO.getContent(),"GB2312") );
        return genAjaxResponse(true,"请求成功！",returnMap);
    }
}
